using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Configuration;
using System.IO;
using System.Xml;
using System.Diagnostics;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Security;
using System.Globalization;

namespace Microsoft.Office.Project.EPMSync.Config.Library
{

    [XmlRootAttribute("SyncConfig", Namespace = "", IsNullable = false)]
    public class SyncConfig
    {
        [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string SchemaVersion;

        [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string PortfolioServerDatabaseConnectionString;

        [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string ProjectServerURL;

        [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public int WakeupInterval;

        [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public int AccountID;

        [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public bool PublishAfterSave;

        [XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public Collection<FieldMapping> FieldMappings;

        [XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        [System.Xml.Serialization.XmlArrayItemAttribute("ExcludedProject", typeof(Guid), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public Collection<Guid> ExcludedProjects;

        #region Utility Methods

        public bool AttributeIsMapped(int attributeId)
        {
            foreach (FieldMapping fm in FieldMappings)
            {
                if (fm.PPSAttributeId == attributeId)
                    return true;
            }
            return false;
        }

        public int GetFieldMappingIndexFromAttributeID(int attributeId)
        {
            for (int i=0; i <= FieldMappings.Count - 1; i++)
            {
                if (FieldMappings[i].PPSAttributeId == attributeId)
                    return i;
            }
            return -1;
        }

        public string GetBuiltInFieldName(int attributeId)
        {
            foreach (FieldMapping fm in FieldMappings)
            {
                if (fm.PPSAttributeId == attributeId)
                    return fm.PSField.BuiltInName;
            }
            return String.Empty;
        }

        public Guid GetCustomFieldGuid(int attributeId)
        {
            foreach (FieldMapping fm in FieldMappings)
            {
                if (fm.PPSAttributeId == attributeId)
                    return fm.PSField.CustomFieldUid;
            }
            return Guid.Empty;
        }

        public bool IsProjectExcluded(Guid projectUid)
        {
            foreach (Guid ep in ExcludedProjects)
            {
                if (ep == projectUid)
                    return true;
            }
            return false;
        }
        #endregion

    }

    public class FieldMapping
    {
        [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public int PPSAttributeId;

        [XmlElementAttribute("PSField", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
        public PSFieldEntry PSField;

        [XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public Collection<LookupMapEntry> LookupMap;
        
        public FieldMapping()
        {
            //PPSAttributeId = 0;
            PSField = new PSFieldEntry();
            PSField.CustomFieldUid = Guid.Empty;
            PSField.BuiltInName = String.Empty;
        }

        public FieldMapping(string ppsAttributeId, string psBuiltInFieldName, Guid psCustomFieldUid)
        {
            PPSAttributeId = Convert.ToInt32(ppsAttributeId, CultureInfo.InvariantCulture);
            PSField = new PSFieldEntry();
            PSField.BuiltInName = psBuiltInFieldName;
            PSField.CustomFieldUid = psCustomFieldUid;
        }

        public int GetLookupTableIndexFromAttributeID(int attributeDefId)
        {
            for (int i = 0; i <= LookupMap.Count - 1; i++)
            {
                if (LookupMap[i].AttributeDefinitionId == attributeDefId)
                    return i;
            }
            return -1;
        }

        public Guid GetPSGuidFromAttributeID(int attributeDefId)
        {
            for (int i = 0; i <= LookupMap.Count - 1; i++)
            {
                if (LookupMap[i].AttributeDefinitionId == attributeDefId)
                    return LookupMap[i].PSLookupTableValueUid;
            }
            return Guid.Empty;
        }
    }

    public class PSFieldEntry
    {
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string BuiltInName;

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public Guid CustomFieldUid;
    }

    public class LookupMapEntry
    {
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public int AttributeDefinitionId;

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public Guid PSLookupTableValueUid;

        public LookupMapEntry()
        {
            //AttributeDefinitionId = 0;
            PSLookupTableValueUid = Guid.Empty;
        }

        public LookupMapEntry(int attributeDefinitionId, Guid lookupTableValueUid)
        {
            AttributeDefinitionId = attributeDefinitionId;
            PSLookupTableValueUid = lookupTableValueUid;
        }
    }


}
